/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package matematicadiscretaxml;

import javax.swing.table.AbstractTableModel;

/**
 *
 * @author diego
 */
public final class ModeloTabla extends AbstractTableModel{

    private String[] columnNames;
    private Object[][] data;
    ObjetoQuery[][] tabla_obj_query = new ObjetoQuery[0][0];

    public ModeloTabla()
    {
        
    }

    public ModeloTabla(ObjetoQuery [][] _tabla_obj_query)
    {
        tabla_obj_query = _tabla_obj_query;
        int numero_filas = _tabla_obj_query.length;
        int numero_columnas = 0;
        if(_tabla_obj_query.length != 0)
        {
            numero_columnas = _tabla_obj_query[0].length;
        }
                
        columnNames = new String[numero_columnas];
        data = new Object[numero_filas][numero_columnas];
        llenarTabla();
    }

    
    public void llenarTabla()
    {
        if(tabla_obj_query.length != 0)
        {
            for(int j = 0; j< data[0].length; j++)
            {
                columnNames[j] = tabla_obj_query[0][j].getColumna();
            }

            for(int i = 0; i< data.length; i++)
            {
                for(int j = 0; j< data[0].length; j++)
                {
                    data[i][j] = tabla_obj_query[i][j].getObjeto().toString();
                }
            }
        }
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col)
    {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
//        if ( col < 1 || row < 1 || col == row )
//        {
            return false;
//        }
//        else
//        {
//            return true;
//        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    @Override
    public void setValueAt(Object value, int row, int col) 
    {
//        int valor_arista = 0;
        if (isCellEditable(row, col))
        {
           System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

//        try
//        {
//            valor_arista = Integer.parseInt(value.toString());
//            data[row][col] = valor_arista ;
//            fireTableCellUpdated(row, col);
//            data[col][row] = valor_arista ;
//            fireTableCellUpdated(col, row);
//        }
//        catch(Exception e)
//        {
//            JOptionPane.showMessageDialog(null, "LAS PONDERACIONES DE LAS ARISTAS A INGRESAR DEBEN SER NUMEROS ENTEROS");
//        }

        if (isCellEditable(row, col)) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData()
    {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}
